﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Fast.Framework.Models;


namespace Fast.Framework.Extensions
{

    /// <summary>
    /// Sql信息扩展类
    /// </summary>
    public static class SqlInfoExtensions
    {

        /// <summary>
        /// 连接格式化
        /// </summary>
        /// <returns></returns>
        public static string JoinFormat(this SqlInfo sqlInfo)
        {
            return string.Join("", sqlInfo.SqlStack.ToList());
        }

        /// <summary>
        /// 条件格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <returns></returns>
        public static string WhereFormat(this SqlInfo sqlInfo)
        {
            return string.Join("", sqlInfo.SqlStack.ToList());
        }

        /// <summary>
        /// In格式化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlInfo">Sql信息</param>
        /// <param name="parameterNotation">参数符号</param>
        /// <param name="sqlParameters">Sql参数</param>
        /// <returns></returns>
        public static string InFormat<T>(this SqlInfo sqlInfo, string parameterNotation, Dictionary<string, T> sqlParameters)
        {
            var sqls = new List<string>();
            var valueStr = string.Join(",", sqlParameters.Keys.Select(s => $"{parameterNotation}{s}"));
            if (sqlInfo.SqlStack.Count > 0)//判断非对象类型
            {
                sqls.Add($"{string.Join("", sqlInfo.SqlStack.ToList())} IN ({valueStr})");
            }
            else
            {
                foreach (var item in sqlInfo.NewValues)
                {
                    sqls.Add($"{item} IN ({valueStr})");
                }
            }
            var sql = string.Join(" OR ", sqls);
            return sql;
        }

        /// <summary>
        /// NotIn格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <param name="parameterNotation">参数符号</param>
        /// <param name="sqlParameters">Sql参数</param>
        /// <returns></returns>
        public static string NotInFormat<T>(this SqlInfo sqlInfo, string parameterNotation, Dictionary<string, T> sqlParameters)
        {
            var sqls = new List<string>();
            var valueStr = string.Join(",", sqlParameters.Keys.Select(s => $"{parameterNotation}{s}"));
            if (sqlInfo.SqlStack.Count > 0)//判断非对象类型
            {
                sqls.Add($"{string.Join("", sqlInfo.SqlStack.ToList())} NOT IN ({valueStr})");
            }
            else
            {
                foreach (var item in sqlInfo.NewValues)
                {
                    sqls.Add($"{item} NOT IN ({valueStr})");
                }
            }
            var sql = string.Join(" AND ", sqls);
            return sql;
        }

        /// <summary>
        /// 分组格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <returns></returns>
        public static string GroupFormat(this SqlInfo sqlInfo)
        {
            var sql = "";
            if (sqlInfo.SqlStack.Count > 0)//判断非对象类型
            {
                sql = string.Join("", sqlInfo.SqlStack.ToList());
            }
            else
            {
                sql = string.Join(",", sqlInfo.NewValues);
            }
            return sql;
        }

        /// <summary>
        /// 有格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <returns></returns>
        public static string HavingFormat(this SqlInfo sqlInfo)
        {
            return string.Join("", sqlInfo.SqlStack.ToList());
        }

        /// <summary>
        /// 排序格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <returns></returns>
        public static string OrderFormat(this SqlInfo sqlInfo)
        {
            var sql = "";
            if (sqlInfo.SqlStack.Count > 0)//判断非对象类型
            {
                sql = string.Join("", sqlInfo.SqlStack.ToList());
            }
            else
            {
                sql = string.Join(",", sqlInfo.NewValues);
            }
            return sql;
        }

        /// <summary>
        /// 选择格式化
        /// </summary>
        /// <param name="sqlInfo">Sql信息</param>
        /// <returns></returns>
        public static string SelectFormat(this SqlInfo sqlInfo)
        {
            var sql = "";
            if (sqlInfo.SqlStack.Count > 0)//判断非对象类型
            {
                sql = string.Join("", sqlInfo.SqlStack.ToList());
            }
            else
            {
                sql = string.Join(",\r\n", sqlInfo.NewAsMapper);
            }
            return sql;
        }
    }
}
